package com.fireghost.jdt.mainActivity;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.fireghost.jdt.R;
import com.fireghost.jdt.database.JobDbEntry;
import com.fireghost.jdt.database.JournalDeTravailDbAdapter;

public class ListViewDailyJobsAdapter extends BaseAdapter implements ListAdapter {

	private ArrayList<JobDbEntry> a_oDailyJobs;
	private Context oContext;
	
	public ListViewDailyJobsAdapter(Context p_oContext) {
		oContext = p_oContext;
		a_oDailyJobs = new ArrayList<JobDbEntry>();
	}
	
	public int getCount() {
		return a_oDailyJobs.size();
	}

	public Object getItem(int p_iPosition) {
		return a_oDailyJobs.get(p_iPosition);
	}

	public long getItemId(int p_iPosition) {
		return a_oDailyJobs.get(p_iPosition).getJobID();
	}

	public View getView(int p_iPosition, View p_oConvertView, ViewGroup p_oParentViewGroup) {
		LinearLayout oLayoutItem;
		
		// Reusing the old layout (gain of speed)
		if (p_oConvertView == null) {
			oLayoutItem = (LinearLayout)LayoutInflater.from(oContext).inflate(R.layout.list_daily_job_item, p_oParentViewGroup, false);
		}
		else {
			oLayoutItem = (LinearLayout)p_oConvertView;
		}
		
		// Get and set the textview
		TextView oTxtTitle = (TextView)oLayoutItem.findViewById(R.id.oTxtItemTitle);
		oTxtTitle.setText(a_oDailyJobs.get(p_iPosition).getStartEndString());
		
		return oLayoutItem;
	}
	
	public void populate(long p_iTimestampDayAtMidnight, JournalDeTravailDbAdapter oJdtDb) {
		a_oDailyJobs = oJdtDb.getDailyJobs(p_iTimestampDayAtMidnight);
		
		this.notifyDataSetChanged();
	}
	
}